//
//  HTTPClient.swift
//  SinaWeibo25
//
//  Created by Apple on 16/3/13.
//  Copyright © 2016年 itcast. All rights reserved.
//  客户端网络请求处理类

import UIKit
import AFNetworking

//线上环境
private let hostname = "https://api.weibo.com/"
//测试环境
//private let hostname = "https://.dev.api.weibo.com/"

//反转域名
private let domain = "com.baidu.data.error"

enum HTTPMethods: String {
    case POST = "POST"
    case GET = "GET"
}

class HTTPClient: AFHTTPSessionManager {

    //使用单例对象来访问 封装的核心方法  伪单例
    static let sharedHttpClient: HTTPClient = {
        //基地址 可以自动拼接前面的基地址
        let url = NSURL(string: hostname)
        let tools = HTTPClient(baseURL: url)
        
        //设置支持的序列化格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //基于网络请求的封装到一个核心方法中
//    AFN.GET(urlStirng, parameters: parameters, progress: nil, success: { (_, result) -> Void in
    //请求字典数据
    func requestJSONDict(method: HTTPMethods, urlString: String,parameters: [String : AnyObject]?,finished: (dict: [String : AnyObject]?, error: NSError?) -> () ) {
        
        if method == .POST {
            //发起POST 请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
                print(result)
                guard let dict = result as? [String : AnyObject] else {
                    //失败需要执行闭包
                    print("数据结构错误")
                    //自定义错误信息
                    //错误的code 系统的error都是 10000以内的  比如 403  404 501  如果自定义错误信息 尽量不要在10000以内
                    let myError = NSError(domain: domain, code: 10001, userInfo: [NSLocalizedDescriptionKey : "数据结构错误"])
                    finished(dict: nil, error: myError)
                    return
                }
                
                //完成成功的回调
                finished(dict: dict, error: nil)
                }, failure: { (_, error) -> Void in
                    //完成失败的回调
                    finished(dict: nil, error: error)
                    print(error)
            })
        } else {
            //发起GET 请求
            
            GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
                print(result)
                guard let dict = result as? [String : AnyObject] else {
                    //失败需要执行闭包
                    print("数据结构错误")
                    //自定义错误信息
                    //错误的code 系统的error都是 10000以内的  比如 403  404 501  如果自定义错误信息 尽量不要在10000以内
                    let myError = NSError(domain: domain, code: 10001, userInfo: [NSLocalizedDescriptionKey : "数据结构错误"])
                    finished(dict: nil, error: myError)
                    return
                }
                
                //完成成功的回调
                finished(dict: dict, error: nil)
                }, failure: { (_, error) -> Void in
                    //完成失败的回调
                    finished(dict: nil, error: error)
                    print(error)
            })
        }
        
    }
    
    
}
